<?php


namespace app\admin\order\logic;


use app\common\logic\BaseLogic;
use app\model\order\OrderDetail;
use app\model\order\OrderRefund;
use support\exception\BadException;
use support\utils\Time;

class OrderRefundLogic extends BaseLogic
{
    public function __construct()
    {
        $this->model = new OrderRefund();
    }

    public function checkRefundMoney($input, $action)
    {
        if ($action == 'agree') {
            //判断退款金额不能大于实际金额

            if ($input['refunded_price'] > $this->model->where(['id' => $input['id']])->value("refund_price")) {
                throw new BadException("实际退款金额不能大于申请金额");
            }

            //同意并退款转账
            $this->model->update([
                'refunded_price' => $input['refunded_price'],
                'status' => OrderRefund::STATUS_PASS,
                'update_time' => Time::now(),
            ], [
                'id' => $input['id'],
                'status' => OrderRefund::STATUS_WAIT,
            ]);

            //发起支付宝或者微信进行退款
        }
        if ($action == 'refuse') {
            //拒绝并记录信息
            $this->model->update([
                'unrefund_reason' => $input['unrefund_reason'],
                'status' => OrderRefund::STATUS_REFUSE,
                'update_time' => Time::now(),
            ], [
                'id' => $input['id'],
                'status' => OrderRefund::STATUS_WAIT,
            ]);
        }
    }

    public function refundPageList($query)
    {
        $query->order("create_time desc");
        $query->with([
            'user' => function ($query) {
                $query->field([
                    "id",
                    "username",
                    "avatar",
                    "mobile",
                ]);
            }
        ]);
        $data = $this->getPageList($query);

        $deIds = [];
        $nums = [];
        foreach ($data['records'] as &$datum) {
            $apply_refund_data = json_decode($datum['apply_refund_data'], true);
            foreach ($apply_refund_data as $vo) {
                $deIds[] = $vo['order_detail_id'];
                $nums[$vo['order_detail_id']] = $vo['num'];
            }
            $datum['detail'] = OrderDetail::where('id', 'in', $deIds)->select()->toArray();
            array_walk($datum['detail'], function (&$v) use ($nums) {
                $v['num'] = $nums[$v['id']];
            });
        }

        return $data;

    }

    public function refundDetail($id)
    {
        $query = $this->search(['id' => $id]);
        $query->with([
            'user' => function ($query) {
                $query->field([
                    "id",
                    "username",
                    "avatar",
                    "mobile",
                ]);
            }
        ]);
        $data = $query->find();

        $deIds = [];
        $nums = [];
        $apply_refund_data = json_decode($data['apply_refund_data'], true);
        foreach ($apply_refund_data as $vo) {
            $deIds[] = $vo['order_detail_id'];
            $nums[$vo['order_detail_id']] = $vo['num'];
        }
        $data['detail'] = OrderDetail::where('id', 'in', $deIds)->select()->toArray();
        array_walk($data['detail'], function (&$v) use ($nums) {
            $v['num'] = $nums[$v['id']];
        });
        return $data;

    }
}